<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2025/4/23
  Time: 20:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>编辑</title>
</head>
<body>
    <%
        /**
         * 以下代码为连接数据库代码
         */
        String dbname = "goods";//数据库名称
        String username = "root";//数据库用户名
        String password = "1234";//数据库密码
        String url =  "jdbc:mysql://localhost:3306/" +dbname;
//    Class.forName("com.mysql.jdbc.Driver");//mysql 5.7一下的版本
        Class.forName("com.mysql.cj.jdbc.Driver");//mysql 8.0
        Connection connection = DriverManager.getConnection(url,username,password);
        Statement statement = connection.createStatement();

        //从另一个页面传递过来的参数
        String stuNo = request.getParameter("stu_no");//学号
        String stuName = request.getParameter("stu_name");//修改后的姓名
        String stuSex = request.getParameter("stu_sex");//性别
        String stu_speciality = request.getParameter("stu_speciality");//输入框里面修改后的专业

        System.out.println(stuName);
        System.out.println(stuNo);
        //执行更新语句
//        int resulet = statement.executeUpdate("update student set stu_name='"+stuName+"',stu_sex='"+stuSex+"' where stu_no='"+stuNo+"'");
        String sql = "update student set stu_name=?,stu_sex=?,stu_speciality=? where stu_no=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,stuName);//数字1表示上方数据库语句中第一个问号要替换的值 stuName
        preparedStatement.setString(2,stuSex);
        preparedStatement.setString(3,stu_speciality);
        preparedStatement.setString(4,stuNo);

        int resulet = preparedStatement.executeUpdate();
        System.out.println(resulet);
        if(resulet>0){//添加成功
            response.sendRedirect("search.jsp");//回到搜索页面
        }
    %>
</body>
</html>
